package com.task;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.pojo.MovieList;
import com.pojo.ProgramCategory;
import com.pojo.Special;
import com.pojo.SpecialFilmList;
import com.service.DyService;
import com.service.DyTypeService;
import com.service.SpecialFilmListService;
import com.service.SpecialService;
import com.util.DesUtil;
import com.util.HardUtil;
import com.util.MD5Util;
import com.util.StringUtils;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.*;

@SuppressWarnings("ALL")
@Component("TimeTaskSendBorastService")
public class TimeTaskSendBorastService {


    @Value("${server.port}")
    private String port;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    @Qualifier("StringRedisTemplate")
    private RedisTemplate redisTemplate;

    @Autowired
    private DyTypeService dyTypeService;

    @Autowired
    private DyService dyService;

    @Value("${server.url}")
    private String server;

    @Autowired
    private RestTemplate restTemplate;


    @Autowired
    private SpecialService specialService;

    @Autowired
    private SpecialFilmListService specialFilmListService;

    @Autowired

    @Value("${dy.tmp}")
    private String tmp;

    @Value("${dy.home}")
    private String home;




    /**
     * 每隔5秒发送广播
     */
    //@Scheduled(cron = "0/5 * * * * ?")
    //@Async
    @PostConstruct
    public void sendBroastMessage() {

        String  broadcast = System.getProperties().getProperty("broadcast");
        if ("false".equals(broadcast))
        {
            return;
        }

        final String port2 = port;
        DatagramSocket ds = null;
        try {
            ds = new DatagramSocket();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        final DatagramSocket ds2 = ds;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    // 广播的实现 :由客户端发出广播，服务器端接收
                    String host = "255.255.255.255";//广播地址
                    int port = 9999;//广播的目的端口
                    String ip = null;
                    try {
                        InputStream is = new FileInputStream("ip.properties");
                        Properties p = new Properties();
                        p.load(is);
                        ip = p.getProperty("server.ip");
                        is.close();
                    } catch (Exception e) {
                        e.printStackTrace();

                    }

                    if (ip != null && ip.indexOf(":") == -1) {
                        ip = ip + ":" + port2;
                    }
                    String message = ip;//用于发送的字符串
                    //System.out.println(message);
                    if (message == null) {
                        return;
                    }
                    try {
                        InetAddress adds = InetAddress.getByName(host);
                        DatagramPacket dp = new DatagramPacket(message.getBytes(),
                                message.length(), adds, port);
                        ds2.send(dp);
                        Thread.sleep(2000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();

    }


}
